package com.tongzhan.service;

import com.tongzhan.base.BaseCriteria;
import com.tongzhan.base.BaseShareService;
import com.tongzhan.consts.SysDictTypeKeyConst;
import com.tongzhan.entity.SysDict;
import com.tongzhan.entity.User;
import com.tongzhan.entity.qiaotai.Personnel;
import com.tongzhan.entity.qiaotai.TownInfo;
import com.tongzhan.utils.ThreadLocalUtil;
import com.tongzhan.vo.PersonnelTownVo;
import com.tongzhan.vo.PersonnelTypeVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author htmic
 * @date 2020/7/31
 */
@Service
public class PersonnelService extends BaseShareService {

    /**
     * 新增城镇信息
     * @param param
     */
    public void addTown(TownInfo param) {
        param.setIs_del(0);
        param.setAddTime(new Date());
        townInfoRepository.insertSelective(param);
    }

    /**
     * 获取 人员信息类型列表
     * @return
     */
    public List<PersonnelTypeVo> getTypeList() {
        //获取人员类型列表
        List<SysDict> typeList = sysDictService.getListByPcode(SysDictTypeKeyConst.QIAOTAI_PERSONNEL_TYPE);
        if (!CollectionUtils.isEmpty(typeList)) {
            List<PersonnelTypeVo> list = new ArrayList<>(typeList.size());
            typeList.forEach(item->{
                PersonnelTypeVo vo = new PersonnelTypeVo();
                vo.setId(Integer.valueOf(item.getType()));
                vo.setTypeName(item.getValue());
                long count = personnelRepository.count(new Query(BaseCriteria.where("is_del").is(0).and("typeId").is(vo.getId())));
                vo.setPersonnelCount(count);
                list.add(vo);
            });
            return list;
        }
        return null;
    }

    /**
     * 获取 人员信息城镇
     * @return
     */
    public List<PersonnelTownVo> getTownList(Integer typeId) {
        //查询所有的城镇信息
        List<TownInfo> townList = townInfoRepository.findAll(new Query().addCriteria(Criteria.where("is_del").is(0)));
        if (!CollectionUtils.isEmpty(townList)) {
            List<PersonnelTownVo> list = new ArrayList<>(townList.size());
            townList.forEach(item->{
                PersonnelTownVo vo = new PersonnelTownVo();
                vo.setId(item.getId());
                vo.setTypeName(item.getName());
                long count = personnelRepository.count(new Query(BaseCriteria.where("is_del").is(0).and("typeId").is(typeId).and("townTypeId").is(vo.getId())));
                vo.setPersonnelCount(count);
                list.add(vo);
            });
            return list;
        }
        return null;
    }

    /**
     * 人员信息列表
     * @param page
     * @param size
     * @return
     */
    public Page<Personnel> getPersonnelList(Integer page, Integer size,String townTypeId,Integer typeId) {
        Query query = new Query(BaseCriteria.where("is_del").is(0).and("townTypeId").is(townTypeId).and("typeId").is(typeId));
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            query.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            query.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "addTime");
        Page<Personnel> all = personnelRepository.findPage(query, of);
        if (!CollectionUtils.isEmpty(all.getContent())) {
            all.getContent().forEach(item->{
                item.setTypeIdName(sysDictService.getValueByType(SysDictTypeKeyConst.QIAOTAI_PERSONNEL_TYPE, item.getTypeId()));
            });
        }
        return all;
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    public Personnel getDetail(String id) {
        Personnel vo = personnelRepository.findByPrimaryKey(id);
        if (vo != null) {
            vo.setTypeIdName(sysDictService.getValueByType(SysDictTypeKeyConst.QIAOTAI_PERSONNEL_TYPE, vo.getTypeId()));
        }
        return vo;
    }

    /**
     * 添加
     * @param entity
     * @return
     */
    public long add(Personnel entity) {
        entity.setIs_del(0);
        entity.setAddTime(new Date());
        User userById = findUserById(entity.getAddUserId());
        if(userById!=null){
            entity.setAddUserName(userById.getUserName());
        }
        TownInfo town = townInfoRepository.findByPrimaryKey(entity.getTownTypeId());
        if (town != null) {
            entity.setTownTypeName(town.getName());
        }
        entity.setStreetCode(ThreadLocalUtil.getThreadLocal().getStreetCode());
        entity.setVillageCode(ThreadLocalUtil.getThreadLocal().getVillageCode());
        long l = personnelRepository.insertSelective(entity);
        if(l>0){
            return  1;
        }
        return 0;
    }

    /**
     * 城镇信息列表
     * @return
     */
    public List<TownInfo> getTownPageList() {
        Query query = new Query(BaseCriteria.where("is_del").is(0)).with(Sort.by(Sort.Direction.DESC, "addTime"));;
        List<TownInfo> all = townInfoRepository.findAll(query);
        return all;
    }
}
